package com.huika.dealontop.views;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * Author: Victor Wu <br/>
 * Email: victor-wu*foxmail.com <br/>
 * Time: 2016/1/14 10:33 <br/>
 * Description: 可监听滑动事件的ScrollView
 */
public class HKScrollView extends PullToZoomScrollView{
    private OnScrollListener onScrollListener;
    /**
     * 主要是用在用户手指离开HKScrollView，HKScrollView还在继续滑动，我们用来保存Y的距离，然后做比较 
     */
    //private int lastScrollY;

    private int scrollYArr[] = new int[3];

    public HKScrollView(Context context) {
        this(context, null);
    }

    public HKScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HKScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * 设置滚动接口 
     * @param onScrollListener
     */
    public void setOnScrollListener(OnScrollListener onScrollListener) {
        this.onScrollListener = onScrollListener;
    }


    /**
     * 用于用户手指离开HKScrollView的时候获取HKScrollView滚动的Y距离，然后回调给onScroll方法中 
     */
    private Handler handler = new Handler() {

        public void handleMessage(android.os.Message msg) {
            int scrollY = HKScrollView.this.getScrollY();
            updateLastScrollY(scrollY);
            if(isScrolling()){
                handler.sendMessageDelayed(handler.obtainMessage(), 5);
            }
//            if(lastScrollY != scrollY){
//                handler.sendMessageDelayed(handler.obtainMessage(), 5);
//                lastScrollY = scrollY;
//            }
            if(onScrollListener != null){
                onScrollListener.onScroll(scrollY);
            }

        };

    };

    private synchronized boolean isScrolling() {
        return scrollYArr[0] != scrollYArr[1] || scrollYArr[0] != scrollYArr[2];
    }

    private synchronized void updateLastScrollY(int lastScrollY) {
        for (int i = 0; i < scrollYArr.length - 1; i++) {
            scrollYArr[i] = scrollYArr[i +1];
        }
        scrollYArr[scrollYArr.length - 1] = lastScrollY;
    }

    /**
     * 重写onTouchEvent， 当用户的手在HKScrollView上面的时候， 
     * 直接将HKScrollView滑动的Y方向距离回调给onScroll方法中，当用户抬起手的时候， 
     * HKScrollView可能还在滑动，所以当用户抬起手我们隔5毫秒给handler发送消息，在handler处理 
     * HKScrollView滑动的距离 
     */
    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        int lastScrollY = this.getScrollY();
        updateLastScrollY(lastScrollY);
        if(onScrollListener != null){
            onScrollListener.onScroll(lastScrollY);
        }
        switch(ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                handler.removeCallbacksAndMessages(null);
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                handler.sendMessageDelayed(handler.obtainMessage(), 5);
                break;

        }
        return super.onTouchEvent(ev);
    }


    /**
     * 滚动的回调接口
     */
    public interface OnScrollListener{
        /**
         * 回调方法， 返回HKScrollView滑动的Y方向距离 
         * @param scrollY
         */
        void onScroll(int scrollY);
    }



}  
